/* jQuery.Spinner V1.0 CopyRight (c) 2014 by:Loyaoo Taobao:http://isseven.taobao.com */

(function($) {
	
	$.fn.Spinner = function (opts) {
	
		var defaults = {value:1, min:1, len:3, max:66}
		var options = $.extend(defaults, opts)
		var keyCodes = {up:38, down:40}
		return this.each(function() {
		
			var a = $('<a></a>'); f(a,0,"Decrease","-");	//加
			var c = $('<a></a>'); f(c,0,"Increase","+");	//减
			var b = $('<input/>');f(b,1,"Amount");cv(0);	//值
			
			$(this).append(a).append(b).append(c);
			a.click(function(){cv(-1)});
			b.keyup(function(){cv(0)});
			c.click(function(){cv(+1)});
			b.bind('keyup paste change',function(e){
				e.keyCode==keyCodes.up&&cv(+1);
				e.keyCode==keyCodes.down&&cv(-1);
			});
			
			function cv(n){
				b.val(b.val().replace(/[^\d]/g,''));
				bv=parseInt(b.val()||options.min)+n;
				bv>=options.min&&bv<=options.max&&b.val(bv);
				if(bv<=options.min){b.val(options.min);f(a,2,"DisDe","Decrease");}else{f(a,2,"Decrease","DisDe");}
				if(bv>=options.max){b.val(options.max);f(c,2,"DisIn","Increase");}else{f(c,2,"Increase","DisIn");}
			}
			
		});

		function f(o,t,c,s){
			t==0&&o.addClass(c).attr("href","javascript:void(0)").append("<i></i>").find("i").append(s);
			t==1&&o.addClass(c).attr({"value":options.value,"autocomplete":"off","maxlength":options.len});
			t==2&&o.addClass(c).removeClass(s);
		}
	}
	
	///new Spinner ,王瑞杭
	$.fn.NewSpinner = function (onChangeFun) {
		
		//
		
		var keyCodes = {up:38, down:40}
		return this.each(function() {
			$this=$(this); 
			if($this.attr('data-name')==undefined)
			{
				$this.attr('data-name','num');
			}	
			if($this.attr('data-min')==undefined)
			{
				$this.attr('data-min','1');
			}
			if($this.attr('data-max')==undefined)
			{
				$this.attr('data-max','99');
			}
			if($this.attr('data-value')==undefined)
			{
				$this.attr('data-value','1');
			}
			
			var a = $('<a></a>'); f(a,0,"Decrease","-");	//加
			var c = $('<a></a>'); f(c,0,"Increase","+");	//减
			var b = $('<input name="'+$this.attr('data-name')+'" value="'+$this.attr('data-value')+'"/>');f(b,1,"Amount");cv(0);//值
			if(onChangeFun)
			{
				b.change(onChangeFun);
			}	
			$(this).append(a).append(b).append(c);
			a.click(function(){cv(-1);});
			b.keyup(function(){cv(0)});
			c.click(function(){cv(+1);});
			b.bind('keyup paste change',function(e){
				e.keyCode==keyCodes.up&&cv(+1);
				e.keyCode==keyCodes.down&&cv(-1);
			});
			
			function cv(n){
			
				b.val(b.val().replace(/[^\d]/g,''));
				bv=parseInt(b.val()||$this.attr('data-min'))+n;
				bv>=$this.attr('data-min')&&bv<=$this.attr('data-max')&&b.val(bv);
				if (bv <= $this.attr('data-min')) {
					b.val($this.attr('data-min'));
					b.change();
					f(a, 2, "DisDe", "Decrease");
					return ;
				} else {
					f(a, 2, "Decrease", "DisDe");
				}
				
				if (bv >= $this.attr('data-max')) {
					b.val($this.attr('data-max'));
					b.change();
					f(c, 2, "DisIn", "Increase");
					return;
				} else {
					f(c, 2, "Increase", "DisIn");
				}
				if(n!=0)
				{
					b.change();
				}	
			}
			function f(o,t,c,s){
				t==0&&o.addClass(c).attr("href","javascript:void(0)").append("<i></i>").find("i").append(s);
				t==1&&o.addClass(c).attr({"autocomplete":"off"});
				t==2&&o.addClass(c).removeClass(s);
			}
			
		});

		
	}
	
	
})(jQuery);